using Lean.Localization;
using System;
using System.Collections.Generic;
using UnityEngine;

public class Achievement : MonoBehaviour
{
	private const string KEY_ACHIEVEMENT_VALIDATED = "{0}_v";

	private const string KEY_ACHIEVEMENT_BEST = "{0}_b";

	private string achievementId;

	public int unlockableSkinId;

	[LeanPhraseName]
	public string description;

	public int objective;

	public bool hasProgression;

	public bool hasBestScore;

	[HideInInspector]
	public List<string> keysString = new List<string>();

	public List<AchievementKeys> keysToUse = new List<AchievementKeys>();

	private void Awake()
	{
		achievementId = $"{GetType()}{GetObjective()}{unlockableSkinId}";
		SetKeysToUse();
	}

	private void OnEnable()
	{
		foreach (string item in keysString)
		{
			AchievementManager.Instance.StartListening(item, this);
		}
	}

	private void OnDisable()
	{
		foreach (string item in keysString)
		{
			AchievementManager.Instance.StopListening(item, this);
		}
	}

	private void SetKeysToUse()
	{
		foreach (AchievementKeys item in keysToUse)
		{
			keysString.Add(item.ToString());
		}
	}

	public virtual int GetCurrentState()
	{
		if (IsValidated())
		{
			return GetObjective();
		}
		return GetRealCurrentState();
	}

	public virtual int GetRealCurrentState()
	{
		return 1;
	}

	public virtual int GetObjective()
	{
		return objective;
	}

	public virtual string GetDescription()
	{
		return description.Replace("{X}", objective.ToString());
	}

	public virtual int GetBest()
	{
		return PlayerPrefs.GetInt($"{achievementId}_b", 0);
	}

	public virtual bool IsValid()
	{
		return GetCurrentState() >= GetObjective();
	}

	public bool IsValidated()
	{
		return PlayerPrefs.HasKey($"{achievementId}_v");
	}

	public void Validate()
	{
		if (!IsValidated() || !PlayerData.Instance.IsSkinUnlocked(unlockableSkinId))
		{
			string value = DateTime.UtcNow.ToShortDateString();
			PlayerPrefs.SetString($"{achievementId}_v", value);
			OnValidation();
		}
	}

	public void CheckValidation()
	{
		if (hasBestScore && GetBest() < GetRealCurrentState())
		{
			PlayerPrefs.SetInt($"{achievementId}_b", GetRealCurrentState());
			PlayerPrefs.Save();
		}
		if (IsValid() && (!IsValidated() || !PlayerData.Instance.IsSkinUnlocked(unlockableSkinId)))
		{
			Validate();
		}
	}

	public virtual void OnValidation()
	{
	}
}
